﻿Public Class Trm

    Private oMaestra As New CMaestras
    Private dtTRM As New DataTable
    Private idTRM As Integer

    Private Sub Trm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        
        Me.dateFecha.Value = Date.Today
        Me.CargarCombos()
        Me.CargarGrilla(Date.Today)
    End Sub

    Private Sub CargarGrilla(ByVal fecha As Date)
        Try
            Dim ds As New DataSet
            ds = Me.oMaestra.GetTRM(CUtil.ConvertirFecha(fecha))
            Me.dtTRM = ds.Tables(0).Copy

            'Llena grilla de las trms de hoy
            If (Me.dtTRM.Rows.Count > 0) Then
                Me.grdTrm.DataSource = Nothing
                Me.grdTrm.Columns.Clear()
                Me.grdTrm.DataSource = Me.dtTRM

                Dim iconoEDITAR As New DataGridViewImageColumn()
                iconoEDITAR.ValuesAreIcons = False
                iconoEDITAR.Image = My.Resources.editar
                iconoEDITAR.HeaderText = "EDITAR"
                iconoEDITAR.Name = "EDITAR"
                Me.grdTrm.Columns.Add(iconoEDITAR)

                Dim iconoELIMINAR As New DataGridViewImageColumn()
                iconoELIMINAR.ValuesAreIcons = False
                iconoELIMINAR.Image = My.Resources.eliminar
                iconoELIMINAR.HeaderText = "ELIMINAR"
                iconoELIMINAR.Name = "ELIMINAR"
                Me.grdTrm.Columns.Add(iconoELIMINAR)

                Me.grdTrm.Columns("ID").Visible = False
                Me.grdTrm.Columns("IDMONEDA1").Visible = False
                Me.grdTrm.Columns("IDMONEDA2").Visible = False
                Me.grdTrm.Columns("EDITAR").Width = 60
                Me.grdTrm.Columns("ELIMINAR").Width = 70
            Else
                Me.grdTrm.DataSource = Nothing
                Me.grdTrm.Columns.Clear()
            End If

        Catch ex As Exception
            MessageBox.Show("Ocurrió un error al cargar la información: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub CargarCombos()
        Try
            Dim ds As New DataSet
            ds = oMaestra.GetTabla(CMaestras.Tabla.Moneda)
            If (ds.Tables(0).Rows.Count > 0) Then                
                Dim dt As DataTable = ds.Tables(0).Copy
                Me.drpMoneda1.DataSource = CUtil.AgregarRegistro(ds.Tables(0), "Seleccione...")
                Me.drpMoneda1.ValueMember = ds.Tables(0).Columns(0).ColumnName
                Me.drpMoneda1.DisplayMember = ds.Tables(0).Columns(1).ColumnName

                Me.drpMoneda2.DataSource = CUtil.AgregarRegistro(dt, "Seleccione...")
                Me.drpMoneda2.ValueMember = dt.Columns(0).ColumnName
                Me.drpMoneda2.DisplayMember = dt.Columns(1).ColumnName
            Else
                MessageBox.Show("No se encontraron monedas. Deben ser creadas primero", "Monedas", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show("Ocurrió un error al cargar las monedas: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub EliminarTrm(ByVal id As Integer)
        Try
            If (MessageBox.Show("¿Realmente desea eliminar este registro?", "Eliminar registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                Cursor = Cursors.WaitCursor
                Me.oMaestra.Eliminar(id, CMaestras.Tabla.TRM)
                MessageBox.Show("El registro ha sido eliminado", "Registro eliminado", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("NO se eliminó el registro: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
        Me.CargarGrilla(Me.dateFecha.Value)
    End Sub

    Private Sub btnCancelarCiudad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelarCiudad.Click
        Me.idTRM = 0
        Me.txtTRM.Text = ""
        If (Me.drpMoneda1.Items.Count = 0) Then
            Me.drpMoneda1.Items.Add("No hay registros")
        End If
        Me.drpMoneda1.SelectedIndex = 0
        If (Me.drpMoneda2.Items.Count = 0) Then
            Me.drpMoneda2.Items.Add("No hay registros")
        End If
        Me.drpMoneda2.SelectedIndex = 0
    End Sub

    Private Function Validar() As Boolean
        Dim mensaje As String = ""

        If (Me.drpMoneda1.SelectedValue = "0" Or Me.drpMoneda2.SelectedValue = "0") Then
            mensaje += "- Debe seleccionar las dos monedas" & Chr(13)
        Else
            'Las dos monedas no pueden ser iguales
            If (Me.drpMoneda1.SelectedValue = Me.drpMoneda2.SelectedValue) Then
                mensaje += "- Debe seleccionar dos monedas diferentes para poder crear la TRM" & Chr(13)
            End If
        End If

        If (Trim(Me.txtTRM.Text).Length = 0) Then
            mensaje += "- Debe escribir el valor" & Chr(13)
        Else
            If Not IsNumeric(Me.txtTRM.Text) Then
                mensaje += "- El valor debe ser numérico" & Chr(13)
            End If
        End If

        If (mensaje.Length > 0) Then
            MessageBox.Show(mensaje, "Datos incompletos", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub btnListoCiudad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListoCiudad.Click
        If (Me.Validar()) Then
            Try
                Cursor = Cursors.WaitCursor
                Dim valor As Double = 0
                If (Me.txtTRM.Text <> "") Then
                    valor = Me.txtTRM.Text
                End If
                Dim result As String = Me.oMaestra.ActualizarTRM(Me.idTRM, CInt(Me.drpMoneda1.SelectedValue), CInt(Me.drpMoneda2.SelectedValue), CUtil.FormatearNumero(Me.txtTRM.Text), CUtil.ConvertirFecha(Me.dateFecha.Value))

                'Si esta abierto el formulario de loas hijas
                If (My.Forms.Hijas.Visible) Then
                    My.Forms.Hijas.RecargarTRM()
                End If
                If (My.Forms.HijasMaritimo.Visible) Then
                    My.Forms.HijasMaritimo.RecargarTRM()
                End If
                If (My.Forms.HijasAereoExportacion.Visible) Then
                    My.Forms.HijasAereoExportacion.RecargarTRM()
                End If
                If (My.Forms.HijasTercero.Visible) Then
                    My.Forms.HijasTercero.RecargarTRM()
                End If
                Cursor = Cursors.Default

                MessageBox.Show(result, "TRM", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                Cursor = Cursors.Default
                MessageBox.Show("Ocurrió un error al guardar la TRM: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try

            Me.idTRM = 0
            Me.txtTRM.Text = ""
            Me.drpMoneda1.SelectedValue = "0"
            Me.drpMoneda2.SelectedValue = "0"
            Me.CargarGrilla(Me.dateFecha.Value)
        End If

    End Sub

#Region "Eventos de la grilla"

    Private Sub grdTrm_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTrm.CellDoubleClick
        '*** Permite EDITAR o ELIMINAR
        'Se obtiene la info del registro
        Dim id As Integer = Me.grdTrm.Rows(e.RowIndex).Cells("ID").Value
        Dim moneda1 As Integer = Me.grdTrm.Rows(e.RowIndex).Cells("IDMONEDA1").Value
        Dim moneda2 As Integer = Me.grdTrm.Rows(e.RowIndex).Cells("IDMONEDA2").Value
        Dim valor As Double = Me.grdTrm.Rows(e.RowIndex).Cells("VALOR").Value

        Select Case Me.grdTrm.Columns(e.ColumnIndex).Name
            Case "EDITAR"
                Me.idTRM = id
                Me.drpMoneda1.SelectedValue = moneda1
                Me.drpMoneda2.SelectedValue = moneda2
                Me.txtTRM.Text = valor
            Case "ELIMINAR"
                Me.EliminarTrm(id)
        End Select

    End Sub

    Private Sub grdTrm_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTrm.CellMouseEnter
        '*** Coloca la manito cuando el mouse entra en las celdas de EDITAR y ELIMINAR
        If (Me.grdTrm.Columns(e.ColumnIndex).Name = "ELIMINAR" Or Me.grdTrm.Columns(e.ColumnIndex).Name = "EDITAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If

    End Sub

    Private Sub grdTrm_CellMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdTrm.CellMouseMove
        If (Me.grdTrm.Columns(e.ColumnIndex).Name = "ELIMINAR" Or Me.grdTrm.Columns(e.ColumnIndex).Name = "EDITAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

#End Region

    Private Sub dateFecha_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dateFecha.ValueChanged
        'Trae las trms registradas en el dia seleccionado
        Me.CargarGrilla(Me.dateFecha.Value)
    End Sub
End Class